﻿
using CommonServiceLocator;
using GalaSoft.MvvmLight.CommandWpf;
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.View;
using Microsoft.Win32;
using Newtonsoft.Json;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows;
using YuanCode.Comm;
using CommonMethods = YuanCode.Comm.CommonMethods;

namespace HuiZe.education.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class ViewModelBase : GalaSoft.MvvmLight.ViewModelBase
    {
        public RelayCommand<string> NavChangedComand { get; set; }
        public RelayCommand<string> ReturnCommand
        {
            get
            {
                return new RelayCommand<string>(new Action<string>((s) =>
                   {
                       MainViewModel mainViewModel = ServiceLocator.Current.GetInstance<MainViewModel>();
                       mainViewModel.NavChanged(s);
                   }));
            }
        }
        public RelayCommand<string> ReturnCommand2
        {
            get
            {
                return new RelayCommand<string>(new Action<string>((s) =>
                {
                    //LoginViewModel loginViewModelModel = ServiceLocator.Current.GetInstance<LoginViewModel>();
                    //loginViewModelModel.NavChanged(s);
                    //Application.Current.s();
                    //HuiZeeducation.Instance.RestartHuiZeeducation();
                    //loginViewModelModel.NavChanged(s);
                    HuiZeeducation.Instance.RestartHuiZeeducation();

                }));
            }
        }
        public RelayCommand ExitCommand { get; set; }
        public double WindowWidth { get; set; }
        public double WindowHeight { get; set; }
        public double PersonPopupWidth { get; set; }
        public double PersonPopupHeight { get; set; }

        public Dictionary<string, FrameworkElement> contents;

        public Rectangle rectangle;
        public ViewModelBase()
        {
            if (IsInDesignMode)
            {
                WindowWidth = 1366;
                WindowHeight = 768;

            }
            contents = new Dictionary<string, FrameworkElement>();
            rectangle = CommonMethods.GetWorkingArea();
            WindowWidth = rectangle.Width;
            WindowHeight = rectangle.Height;

            ExitCommand = new RelayCommand(Exit);
            NavChangedComand = new RelayCommand<string>(NavChanged);

        }
        public ResLoginDeatail2 resLoginDeatail { get; set; }
        public virtual void NavChanged(string viewName)
        {

        }

        public virtual void Return(string viewName)
        {

        }

        private void Exit()
        {
            Application.Current.Shutdown();
        }

        public FrameworkElement GetFrameworkElement(string viewName)
        {
            Type type = Type.GetType($"HuiZe.education.View.{viewName}");
            if (type != null)
                return (FrameworkElement)Activator.CreateInstance(type);
            else
                return null;
        }

        public string SelectFile(double fileLenght = -1)
        {
            string fileName = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择图片";
            dialog.Filter = "图片文件(*.jpg,*.gif,*.bmp,*.png)|*.jpg;*.gif;*.bmp;*.png";
            if (dialog.ShowDialog() == true)
            {
                fileName = dialog.FileName;
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                if (fileLenght > 0 && fileInfo.Length / 1024 > fileLenght)
                {
                    MessageBox.Show("图片超过2M，请重新选择");
                    return "";
                }
            }
            return fileName;
        }

        public string SaveFile()
        {
            string fileName = "";
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型
            sfd.Filter = "*|*.xlsx";

            //设置默认文件类型显示顺序
            sfd.FilterIndex = 1;
            sfd.FileName = "学生信息";
            //保存对话框是否记忆上次打开的目录
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == true)
            {
                fileName = sfd.FileName;

            }
            return fileName;
        }
        public string SaveZip()
        {
            string fileName = "";
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型
            sfd.Filter = "*|*.zip";

            //设置默认文件类型显示顺序
            sfd.FilterIndex = 1;
            sfd.FileName = "备课11";
            //保存对话框是否记忆上次打开的目录
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == true)
            {
                fileName = sfd.FileName;

            }
            return fileName;
        }
        public string SaveZip(string _path)
        {
            string fileName = "";
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型
            sfd.Filter = "Zip包|*.zip";

            //设置默认文件类型显示顺序
            sfd.FilterIndex = 1;
            sfd.FileName = Path.GetFileName(_path);
            //保存对话框是否记忆上次打开的目录
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == true)
            {
                fileName = sfd.FileName;
            }
            return fileName;
        }
        public string SelectFile2(double fileLenght = -1)
        {
            string fileName = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "*|*.xlsx";
            if (dialog.ShowDialog() == true)
            {
                fileName = dialog.FileName;
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                //if (fileLenght > 0 && fileInfo.Length / 1024 > fileLenght)
                //{
                //    //MessageBox.Show("超过2M，请重新选择");
                //    return "";
                //}
            }
            return fileName;
        }
        public string SelectAllFile(double fileLenght = -1)
        {
            string fileName = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "*|*";
            if (dialog.ShowDialog() == true)
            {
                fileName = dialog.FileName;
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                //if (fileLenght > 0 && fileInfo.Length / 1024 > fileLenght)
                //{
                //    //MessageBox.Show("超过2M，请重新选择");
                //    return "";
                //}
            }
            return fileName;
        }

        public void ShowDialogView(string content)
        {
            ShowDialogView showDialogView = new ShowDialogView() { ShowDialogContent = content };
            showDialogView.ShowDialog();
        }

        public bool ShowDialogView_Ethan(string content)
        {
            ShowDialogView_Ethan showDialogView = new ShowDialogView_Ethan(content);
            return showDialogView.ShowDialog() == true;
        }

        public Tuple<bool, RequestModel<T>> Request<T>(string url, string data, string type)
        {
            try
            {
                var res = WebApiHelper.HttpPost(url, data);
                if (res.Item1)
                {
                    var request = JsonHelper.FromJson<RequestModel<T>>(res.Item2);
                    if (request != null)
                    {
                        if (request.code == 1)
                        {
                            return Tuple.Create(true, request);
                        }
                        return Tuple.Create(false, request);
                    }
                    else
                    {
                        ShowDialogView($"{type}失败");
                        return Tuple.Create<bool, RequestModel<T>>(false, null);
                    }
                }
                else
                {
                    ShowDialogView($"{type}失败");
                    return Tuple.Create<bool, RequestModel<T>>(false, null);
                }
            }
            catch
            {
                ShowDialogView($"{type}失败");
                return Tuple.Create<bool, RequestModel<T>>(false, null);
            }
        }

        public Tuple<bool, RequestModel2<T>> Request2<T>(string url, string data, string type)
        {
            try
            {
                var res = WebApiHelper.HttpPost(url, data);
                if (res.Item1)
                {
                    var jsonResult = JsonConvert.DeserializeObject(res.Item2.ToString()).ToString();
                    var request = JsonHelper.FromJson<RequestModel2<T>>(jsonResult);
                    if (request != null)
                    {
                        if (request.code == 1)
                        {
                            return Tuple.Create(true, request);
                        }
                        return Tuple.Create(false, request);
                    }
                    else
                    {
                        ShowDialogView($"{type}失败");
                        return Tuple.Create<bool, RequestModel2<T>>(false, null);
                    }
                }
                else
                {
                    ShowDialogView($"{type}失败");
                    return Tuple.Create<bool, RequestModel2<T>>(false, null);
                }
            }
            catch
            {
                ShowDialogView($"{type}失败");
                return Tuple.Create<bool, RequestModel2<T>>(false, null);
            }
        }
        public Tuple<bool, RequestModel<T>> Request3<T>(string url, string data, string type)
        {
            try
            {
                var res = WebApiHelper.HttpPost(url, data);
                if (res.Item1)
                {

                    var request = JsonHelper.FromJson<RequestModel<T>>(res.Item2);
                    if (request != null)
                    {
                        if (request.code == 1)
                        {
                            return Tuple.Create(true, request);
                        }
                        return Tuple.Create(false, request);
                    }
                    else
                    {
                        resLoginDeatail = JsonHelper.FromJson<ResLoginDeatail2>(res.Item2);
                        //ShowDialogView($"{type}失败");
                        return Tuple.Create<bool, RequestModel<T>>(false, null);
                    }
                }
                else
                {
                    //ShowDialogView($"{type}失败");
                    return Tuple.Create<bool, RequestModel<T>>(false, null);
                }
            }
            catch
            {
                //ShowDialogView($"{type}失败");
                return Tuple.Create<bool, RequestModel<T>>(false, null);
            }
        }
        public Tuple<bool, RequestModel3<T>> Request4<T>(string url, string data, string type)
        {
            try
            {
                var res = WebApiHelper.HttpPost(url, data);
                if (res.Item1)
                {

                    var request = JsonHelper.FromJson<RequestModel3<T>>(res.Item2);
                    if (request != null)
                    {
                        if (request.code == 1)
                        {
                            return Tuple.Create(true, request);
                        }
                        return Tuple.Create(false, request);
                    }
                    else
                    {
                        //ShowDialogView($"{type}失败");
                        return Tuple.Create<bool, RequestModel3<T>>(false, null);
                    }
                }
                else
                {
                    //ShowDialogView($"{type}失败");
                    return Tuple.Create<bool, RequestModel3<T>>(false, null);
                }
            }
            catch
            {
                //ShowDialogView($"{type}失败");
                return Tuple.Create<bool, RequestModel3<T>>(false, null);
            }
        }
        public string RequestDown(string url, string data, string type)
        {
            try
            {
                var res = WebApiHelper.HttpPost(url, data);
                if (res.Item1)
                {
                    return res.Item2.ToString();
                }
                else
                {
                    ShowDialogView($"{type}失败");
                    return "";
                }
            }
            catch
            {
                ShowDialogView($"{type}失败");
                return "";
            }
        }

        public bool RequestFile(string url, string type, string path)
        {
            var res = WebApiHelper.Download(url, path);
            if (res)
            {
                return res;
            }
            else
            {
                ShowDialogView($"{type}失败");
                return false;
            }
        }
        public string Request2(string url, string type)
        {

            string res = WebApiHelper.HttpPost2(url, "");
            if (!string.IsNullOrWhiteSpace(res))
            {

                return res;
            }
            else
            {
                ShowDialogView($"{type}失败");
                return "";
            }
        }

        public Tuple<bool, RequestModel<T>> Upload<T>(string url, string data, string type, string path)
        {
            var res = WebApiHelper.HttpUploadFile(url, path);
            if (!string.IsNullOrWhiteSpace(res))
            {
                var request = JsonHelper.FromJson<RequestModel<T>>(res.ToString());
                if (request.code == 1)
                {
                    return Tuple.Create(true, request);
                }
                return Tuple.Create(false, request);
            }
            else
            {
                ShowDialogView($"{type}失败");
                return Tuple.Create<bool, RequestModel<T>>(false, null);
            }
        }
        public Tuple<bool, RequestModel<T>> Upload<T>(string url, string data, string type, string path, Action<long, long> progress)
        {
            var res = WebApiHelper.HttpUploadFile(url, path, progress);
            if (!string.IsNullOrWhiteSpace(res))
            {
                var request = JsonHelper.FromJson<RequestModel<T>>(res.ToString());
                if (request.code == 1)
                {
                    return Tuple.Create(true, request);
                }
                return Tuple.Create(false, request);
            }
            else
            {
                ShowDialogView($"{type}失败");
                return Tuple.Create<bool, RequestModel<T>>(false, null);
            }
        }

    }
}
